<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>实例管理</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <script src="/qrcode/jquery-3.5.1.js"></script>
    <link rel="stylesheet" href="/layer-css.css"/>
    <style>
        .pear-container {
            margin: 15px;
            width: calc(100vw - 30px);
        }

        .layui-table-box button {
            font-size: 14px !important;
            height: 25px;
            line-height: 23px;
        }

        .layui-form-item .layui-inline {
            margin-bottom: 10px;
        }

        .layui-form-label {
            padding: 9px 8px;
            width: 60px;
        }
    </style>
</head>
<body class="pear-container">
<div class="layui-card searchCriteria">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">连接名称</label>
                    <div class="layui-input-inline">
                        <select name="nodeId">
                            <option value="" selected>请选择</option>
                            <option th:each="item : ${nodeList}" th:value="${item.id}" th:text="${item.name}"></option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-inline">
                        <select name="status">
                            <option value="" selected>请选择</option>
                            <option value="1">运行中</option>
                            <option value="0">已停止</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit
                            lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="mysqlTable" lay-filter="mysqlTable"></table>
    </div>
</div>
<script type="text/html" id="user-bar">
    <!--    <button class="pear-btn pear-btn-primary pear-btn-sm" style="background-color: #2DBBF0 !important;-->
    <!--                              border: 1px solid #2DBBF0 !important;" lay-event="detail">-->
    <!--        详情-->
    <!--    </button>-->
    <!--    <button class="pear-btn pear-btn-primary pear-btn-sm" style="background-color: #398FFF !important;-->
    <!--                              border: 1px solid #398FFF !important;" lay-event="download">-->
    <!--        下载-->
    <!--    </button>-->
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
        删除
    </button>
</script>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script type="text/html" id="taskStatus">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="运行中|已停止"
           lay-filter="taskStatus" {{ d.status== 1 ? 'checked' : '' }} />
</script>
<script type="text/javascript" th:inline="javascript">
    let nginxPath = [[${nginxPath}]];


    layui.use(['jquery', 'form', 'table', 'layer'], function () {
        const form = layui.form,
            table = layui.table,
            layer = layui.layer;
        let cols = [
            [
                {field: 'node_name', title: '连接名称',align: 'center'},
                {field: 'cron_text', title: '执行时间',align: 'center',width: 250},
                {
                    field: 'status', title: '状态',align: 'center', templet: '#taskStatus'
                },
                {field: 'database_name', title: '数据库名',align: 'center'},
                {field: 'tables_name', title: '数据表名',align: 'center'},
                {field: 'remark', title: '备注',align: 'center'},
                {field: 'create_time', minWidth: 180, title: '创建时间',align: 'center'},
                {title: '操作', minWidth: 150, toolbar: '#user-bar', fixed: 'right', align: 'center'}
            ]
        ]

        tableIns = table.render({
            elem: '#mysqlTable',
            url: '/backup/task/list',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            id: 'mysqlTable',
            height: 'full-178   ',
            //解决页码的问题
            done: function (res) {
                // 获得当前页码
                var brforeCurr = tableIns.config.page.curr;
                // 获得当前页的记录数
                var dataLength = table.cache['mysqlTable'].length;
                var count = res.count; // 获得总记录数
                //如果当前页的记录数为0并且总记录数不为0
                if (dataLength == 0 && count != 0) {
                    // 刷新表格到上一页
                    table.reload("mysqlTable", {
                        page: {
                            curr: brforeCurr - 1
                        }
                    });
                }
            }
        });

        // 单选框事件
        form.on('switch(taskStatus)', function (obj) {
            layer.confirm(obj.elem.checked ? '确定要启用该定时任务吗' : '确定要停用该定时任务吗', {
                icon: 3,
                title: ['提示', 'font-size: 18px;'],
                shade: 0.5,
                move: false,
                area: ['499px', '303px'],
                id: "borrowingConfirm"
            }, function (index) {
                layer.close(index);
                var status = obj.elem.checked ? 1 : 0;
                var data = {
                    id: obj.value,
                    status: status
                }
                let loading = layer.load();
                $.ajax({
                    url: '/backup/task/changeStatus',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'put',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code === 200) {
                            layer.msg(result.message, {icon: 1, time: 2000}, function () {});
                        } else {
                            layer.msg(result.message, {icon: 2, time: 2000},function () {
                                window.refresh();
                            });
                        }
                    }
                })
            });
        });

        table.on('tool(mysqlTable)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'detail') {
                window.detail(obj);
            } else if (obj.event === 'download') {
                window.download(obj);
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        table.on('toolbar(mysqlTable)', function (obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            }
        });


        window.remove = function (obj) {
            layer.confirm('确定要删除该定时任务吗', {
                icon: 3,
                title: ['提示', 'font-size: 18px;'],
                shade: 0.5,
                move: false,
                area: ['499px', '303px'],
                id: "borrowingConfirm"
            }, function (index) {
                layer.close(index);
                $.ajax({
                    url: '/backup/task/delete',
                    type: 'post',
                    data: JSON.stringify({id: obj.data.id}),
                    dataType: 'json',
                    contentType: "application/json;charset=utf-8",
                    success: function (res) {
                        if (res.code === 200) {
                            layer.msg(res.message, {icon: 1});
                            table.reload('mysqlTable');
                        } else {
                            layer.msg(res.message, {icon: 2});
                        }
                    }
                });
            });
        }

        window.detail = function (obj) {
            parent.layer.open({
                title: ['修改', 'font-size: 18px;'],
                type: 2,
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                area: ['40%', '100%'],
                shade: 0.5,
                shadeClose: true,
                maxmin: true,
                id: 'addNewEquipment',
                content: '/backup/node/info/update/' + obj.data.id,
                end: function () {
                    table.reload('mysqlTable');
                }
            });
        }

        window.download = function (obj) {
            console.log(nginxPath + obj.data.file_path);
            window.open(nginxPath + obj.data.file_path, '_blank');
        }


        window.refresh = function (param) {
            table.reload('mysqlTable');
        }

        form.on('submit(user-query)', function (data) {
            table.reload('mysqlTable', {
                where: data.field,
                page: {
                    curr: 1 // 重新从第 1 页开始
                }
            });
            return false;
        });

    })
</script>
</body>
</html>